package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.channel.Rank;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/6/27.
 */
public class RankLinearLayoutAdapter extends RecyclerView.Adapter<RankLinearLayoutAdapter.RankViewHolder> {

    private Context context;

    private LayoutInflater inflater;

    private List<Rank> rankList;

    private mItemOnClickListener itemOnClickListener;

    public RankLinearLayoutAdapter(Context context, List<Rank> rankList) {
        this.context = context;
        this.rankList = rankList;

        inflater = LayoutInflater.from(context);
    }

    /**
     * 设置监听
     * @param itemOnClickListener
     */
    public void setItemOnClickListener(mItemOnClickListener itemOnClickListener)
    {
        this.itemOnClickListener = itemOnClickListener;
    }

    @Override
    public RankViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = inflater.inflate(R.layout.rank_item,null);

        return new RankViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RankViewHolder holder, final int position) {

        Rank rank  = rankList.get(position);

        ImageLoader.getInstance().displayImage(rank.getImage_url(), holder.imageView, ImageUtil.getDefaultOptions());

        holder.tvtitle.setText(rank.getTitle());

        List<String> subvalues = rank.getSubValues();
        holder.tvsubvalue1.setText("1."+subvalues.get(0));
        holder.tvsubvalue2.setText("2."+subvalues.get(1));
        holder.tvsubvalue3.setText("3."+subvalues.get(2));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                itemOnClickListener.OnItemClick(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return rankList == null ? 0 : rankList.size();
    }

    public class RankViewHolder extends RecyclerView.ViewHolder
    {
        TextView tvtitle,tvsubvalue1,tvsubvalue2,tvsubvalue3;
        ImageView imageView;

        public RankViewHolder(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.rank_item_main_iv);

            tvtitle = (TextView) itemView.findViewById(R.id.rank_item_title_tv);
            tvsubvalue1 = (TextView) itemView.findViewById(R.id.rank_item_subvalues_tv1);
            tvsubvalue2 = (TextView) itemView.findViewById(R.id.rank_item_subvalues_tv2);
            tvsubvalue3 = (TextView) itemView.findViewById(R.id.rank_item_subvalues_tv3);
        }
    }

    public interface mItemOnClickListener
    {
        void OnItemClick(int position);
    }
}
